package ace.module.product.core.impl.manager.impl;

import ace.cmp.core.constants.CoreConstant;
import ace.cmp.data.spring.data.jdbc.querydsl.rsql.core.model.SaveContext;
import ace.cmp.data.spring.data.jdbc.querydsl.rsql.core.model.SaveListContext;
import ace.cmp.data.spring.data.jdbc.querydsl.rsql.core.utils.SaveContextUtils;
import ace.module.common.core.impl.manager.impl.AbstractEntityOfAppManager;
import ace.module.product.api.enums.PmsCommonGalleryObjectTypeEnum;
import ace.module.product.api.model.dto.PmsCommonGalleryDto;
import ace.module.product.core.impl.dao.entity.PmsCommonGallery;
import ace.module.product.core.impl.manager.PmsCommonGalleryManager;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.LongStream;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * 商品应用通用相册 spring data jdbc querydsl rsql manager implement.
 *
 * @author Caspar
 */
@Slf4j
@AllArgsConstructor
@Component
public class PmsCommonGalleryManagerImpl
    extends AbstractEntityOfAppManager<PmsCommonGallery, Long, PmsCommonGalleryDto>
    implements PmsCommonGalleryManager {


  @Override
  public PmsCommonGallery assembleProductMainImage(Long pmsAppId, Long productId, String imgUrl) {
    return this.build(pmsAppId, pmsAppId, productId, PmsCommonGalleryObjectTypeEnum.PRODUCT, imgUrl, CoreConstant.DEFAULT_SORT);
  }

  @Override
  public List<PmsCommonGallery> assembleProductGalleries(Long pmsAppId, Long productId, List<String> imgUrls) {
    if (CollectionUtils.isEmpty(imgUrls)) {
      return new ArrayList<>();
    }

    return LongStream.range(0, imgUrls.size())
        .mapToObj(index -> {
          String imgUrl = imgUrls.get((int) index);
          return this.build(pmsAppId, pmsAppId, productId, PmsCommonGalleryObjectTypeEnum.PRODUCT_GALLERY, imgUrl, index);
        })
        .collect(Collectors.toList());
  }

  @Override
  public SaveContext<PmsCommonGallery> assembleProductMainImageSaveContext(Long pmsAppId, Long productId, String imgUrl) {
    PmsCommonGallery gallery = this.assembleProductMainImage(pmsAppId, productId, imgUrl);
    return SaveContextUtils.of(gallery, true);
  }

  @Override
  public SaveListContext<PmsCommonGallery> assembleProductGalleriesSaveListContext(Long pmsAppId, Long productId, List<String> imgUrls) {
    List<PmsCommonGallery> galleries = this.assembleProductGalleries(pmsAppId, productId, imgUrls);
    return SaveContextUtils.of(galleries, true);
  }

  @Override
  public SaveContext<PmsCommonGallery> assembleProductSkuMainImageSaveContext(Long pmsAppId, Long productId, Long productSkuId, String imgUrl) {
    PmsCommonGallery gallery = this.build(pmsAppId, productId, productSkuId, PmsCommonGalleryObjectTypeEnum.PRODUCT_SKU, imgUrl, CoreConstant.DEFAULT_SORT);
    return SaveContextUtils.of(gallery, true);
  }

  private PmsCommonGallery build(long pmsAppId, long objectParentId, long objectId, PmsCommonGalleryObjectTypeEnum typeEnum, String imgUrl, Long sort) {
    if (StringUtils.isEmpty(imgUrl)) {
      return null;
    }
    return PmsCommonGallery.builder()
        .id(this.getNewId())
        .pmsAppId(pmsAppId)
        .objectParentId(objectParentId)
        .objectId(objectId)
        .objectType(typeEnum.getCode())
        .picOriginal(imgUrl)
        .picBig(imgUrl)
        .picSmall(imgUrl)
        .picThumbnail(imgUrl)
        .sort(sort)

        .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
        .createTime(System.currentTimeMillis())
        .updateTime(System.currentTimeMillis())
        .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
        .build();
  }
}
